from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QGroupBox, QVBoxLayout, QHBoxLayout, QLabel, QSpinBox, QLineEdit, QPushButton, QFormLayout


class CaptureInfoBox(QGroupBox):
    def __init__(self, title="截图信息"):
        super().__init__(title)

        # 设置布局
        main_layout = QVBoxLayout()
        self.setLayout(main_layout)

        # 创建一个紧凑的表单布局
        compact_form_layout = QFormLayout()

        # 宽度、高度输入框
        width_label = QLabel("宽度")
        self.width_spinbox = QSpinBox()
        self.width_spinbox.setMaximum(5000)  # 设置默认最大值
        self.width_spinbox.setFixedWidth(100)  # 设置固定宽度
        
        height_label = QLabel("高度")
        # 设置右对齐
        height_label.setAlignment(Qt.AlignmentFlag.AlignRight)
        self.height_spinbox = QSpinBox()
        self.height_spinbox.setMaximum(5000)  # 设置默认最大值
        self.height_spinbox.setFixedWidth(100)  # 设置固定宽度

        # 使用 QHBoxLayout 将宽度和高度放在同一行
        size_layout = QHBoxLayout()
        size_layout.addWidget(width_label)
        size_layout.addWidget(self.width_spinbox)
        size_layout.addWidget(height_label)
        size_layout.addWidget(self.height_spinbox)
        compact_form_layout.addRow(size_layout)

        # X坐标、Y坐标输入框
        x_label = QLabel("X坐标")
        self.x_spinbox = QSpinBox()
        self.x_spinbox.setMaximum(5000)  # 设置默认最大值
        self.x_spinbox.setFixedWidth(100)  # 设置固定宽度

        y_label = QLabel("Y坐标")
        self.y_spinbox = QSpinBox()
        self.y_spinbox.setMaximum(5000)  # 设置默认最大值
        self.y_spinbox.setFixedWidth(100)  # 设置固定宽度

        # 使用 QHBoxLayout 将 X 坐标和 Y 坐标放在同一行
        coord_layout = QHBoxLayout()
        coord_layout.addWidget(x_label)
        coord_layout.addWidget(self.x_spinbox)
        coord_layout.addWidget(y_label)
        coord_layout.addWidget(self.y_spinbox)
        compact_form_layout.addRow(coord_layout)

        # recordPos 和 截图名称 输入框
        record_pos_label = QLabel("recordPos")
        self.record_pos_edit = QLineEdit()
        self.record_pos_edit.setReadOnly(True)
        compact_form_layout.addRow(record_pos_label, self.record_pos_edit)

        screenshot_name_label = QLabel("截图名称")
        self.screenshot_name_edit = QLineEdit()
        compact_form_layout.addRow(screenshot_name_label, self.screenshot_name_edit)

        # 添加紧凑表单布局到主布局中
        main_layout.addLayout(compact_form_layout)

        # 保存截图 和 测试模板 按钮
        button_layout = QHBoxLayout()
        save_screenshot_button = QPushButton("保存截图")
        test_template_button = QPushButton("测试模板")
        button_layout.addWidget(save_screenshot_button)
        button_layout.addWidget(test_template_button)

        main_layout.addLayout(button_layout)
        
        # 保存图片尺寸信息
        self.image_width = 5000  # 默认图片宽度
        self.image_height = 5000  # 默认图片高度
    
    def update_info(self, width, height, x, y, record_pos, name):
        """更新截图信息显示"""
        print(f"Updating capture info - Width: {width}, Height: {height}, X: {x}, Y: {y}")
        self.width_spinbox.setValue(width)
        self.height_spinbox.setValue(height)
        self.x_spinbox.setValue(x)
        self.y_spinbox.setValue(y)
        self.record_pos_edit.setText(record_pos)
        self.screenshot_name_edit.setText(name)
        
    def set_image_size(self, width, height):
        """设置图片尺寸，用于更新SpinBox的最大值"""
        try:
            print(f"Setting image size - Width: {width}, Height: {height}")
            self.image_width = width
            self.image_height = height
            self.width_spinbox.setMaximum(width)
            self.height_spinbox.setMaximum(height)
            self.x_spinbox.setMaximum(width)
            self.y_spinbox.setMaximum(height)
            print("Image size set successfully")
        except Exception as e:
            print(f"Error in set_image_size: {e}")
            import traceback
            traceback.print_exc()
